#include "publishblog.h"

PublishBlog::PublishBlog(QWidget *parent) : QWidget(parent)
{
    QVBoxLayout *vLayout = new QVBoxLayout();
    vLayout->setMargin(0);
    QHBoxLayout *hLayout = new QHBoxLayout();
    hLayout->setMargin(0);
    QLabel *label = new QLabel("标题");
    lineTitle = new QLineEdit();

    // 标题栏
    hLayout->addWidget(label);
    hLayout->addWidget(lineTitle);
    QWidget *widget = new QWidget();
    widget->setLayout(hLayout);
    vLayout->addWidget(widget);

    // 插图工具
    labelPix = new QLabel();
    labelPix->installEventFilter(this);
    QPixmap pix(":/res/pic.jpeg");
    QPixmap dst = pix.scaled(35,35,Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
    labelPix->setPixmap(dst);
    vLayout->addWidget(labelPix);

    // 正文
    textContent = new QTextEdit();
    textContent->setMinimumHeight(800);
    vLayout->addWidget(textContent);

    // 发布
    QWidget *widget1 = new QWidget();
    QSpacerItem *spacer = new QSpacerItem(20, 40, QSizePolicy::Expanding, QSizePolicy::Ignored);
    QHBoxLayout *hLayout1 = new QHBoxLayout();
    hLayout1->setMargin(0);
    // 付费
    comboBoxPay = new QComboBox();
    connect(comboBoxPay, SIGNAL(currentIndexChanged(int)), SLOT(setPay(int)));
    comboBoxPay->addItem("免费");
    comboBoxPay->addItem("付费");
    // 文章类别
    QList<QString> strList;
    strList << "C" << "C++" << "PHP" << "Java" << "Android" << "HmarmonyOS" << "STM32";
    comboboxType = new QComboBox();
    connect(comboboxType, SIGNAL(currentIndexChanged(int)), SLOT(setType(int)));
    for(int i=0; i<strList.length(); i++)
    {
       comboboxType->addItem(strList.at(i));
    }
    btnPublish = new QPushButton("发布作品");
    connect(btnPublish, SIGNAL(clicked()), this, SLOT(slotBtnPublish()));

    hLayout1->addItem(spacer);
    QLabel *label1 = new QLabel("文章权限");
    hLayout1->addWidget(label1);
    hLayout1->addWidget(comboBoxPay);
    hLayout1->addWidget(comboboxType);
    hLayout1->addWidget(btnPublish);
    widget1->setLayout(hLayout1);
    vLayout->addWidget(widget1);

    setLayout(vLayout);
}

bool PublishBlog::eventFilter(QObject *obj, QEvent *event)
{
    if(obj == labelPix)
    {
        if(event->type() == QEvent::MouseButtonPress)
        {
            QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
            // 左键单击
            if(mouseEvent->button() == Qt::LeftButton)
            {
                // 打开文件选择框
                pixName = QFileDialog::getOpenFileName(this, tr("打开图片"), "/", tr("图片(*.png *.jpeg *.jpg)"));
                if(!pixName.isEmpty())
                {
                    // 数量增加
                    pixIdx++;
                    // 保存名字
                    nameList << pixName;
                    // 获取图片
                    insertPix = QPixmap(pixName);
                    // 图片宽度、高度
                    pixWidth=insertPix.width();
                    pixHeight = insertPix.height();
                    // 允许的最大宽度
                    maxWidth = this->width();
                    if(pixWidth>maxWidth)
                    {
                        pixHeight = (pixHeight / pixWidth) * maxWidth;
                        pixWidth = maxWidth;
                    }
                    // 设置图片居中，使用html语言
                    QString str = "<p align='center'><img src='" + pixName + "' alt='抱歉!图片消失了' width='" + QString::number(pixWidth) + "' height='"+QString::number(pixHeight) + "'/></p><br>";
                    textContent->append(str);
                    textContent->append("[" + pixName + "]\n");
                }
                return true;
            }
            else
            {
                return false;
            }
        }
    }
    return QObject::eventFilter(obj, event);
}

void PublishBlog::slotBtnPublish()
{
    QMap<QString, QString> mapInfo;
    mapInfo.insert("blog_title", this->lineTitle->text());
    //mapInfo.insert("blog_content", this->textContent->toHtml());
    mapInfo.insert("blog_content", this->textContent->toPlainText());
    mapInfo.insert("blog_pay", QString::number(comboBoxPay->currentIndex()));
    mapInfo.insert("blog_type", comboboxType->currentText());

    emit signalPublishBlog(mapInfo, this->nameList);
}

void PublishBlog::slotInsertPix()
{

}

void PublishBlog::setPay(int pay)
{
    this->isPay = pay;
}

void PublishBlog::setType(int type)
{
    this->blogType = comboboxType->currentText();
}
